package com.boil.model;

import com.boil.util.Constants;

import java.util.Set;
import java.util.TreeSet;

/**
 * 菜单信息实体类。
 *
 * @author ChenYong
 * @date 2016-10-10
 */
public class MenuInfo implements Comparable<MenuInfo> {
    /**
     * 菜单编号
     */
    private Long id;
    /**
     * 父菜单编号
     */
    private Long parentId;
    /**
     * 菜单名称
     */
    private String name;
    /**
     * 菜单 URL
     */
    private String url;
    /**
     * 菜单图标
     */
    private String icon;
    /**
     * 菜单的层级
     */
    private Integer level;
    /**
     * 菜单的显示顺序：0-不按顺序显示；否则，值越小，越排在前
     */
    private Integer sequence;
    /**
     * 父菜单
     */
    private MenuInfo parent;
    /**
     * 子菜单列表
     */
    private Set<MenuInfo> childs;

    // 普通初始化块
    {
        childs = new TreeSet<MenuInfo>();
    }

    /**
     * 默认构造器。
     */
    public MenuInfo() {
        super();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    public Integer getSequence() {
        return sequence;
    }

    public void setSequence(Integer sequence) {
        this.sequence = sequence;
    }

    public MenuInfo getParent() {
        return parent;
    }

    public void setParent(MenuInfo parent) {
        this.parent = parent;
    }

    public Set<MenuInfo> getChilds() {
        return childs;
    }

    public void setChilds(Set<MenuInfo> childs) {
        this.childs = new TreeSet<MenuInfo>(childs);
    }

    @Override
    public int compareTo(MenuInfo o) {
        if ((this == null) || (o == null)) {
            return Constants.VALUE_0;
        }

        return this.getSequence().compareTo(o.getSequence());
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MenuInfo other = (MenuInfo) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("MenuInfo{");
        sb.append("id=").append(id);
        sb.append(", parentId=").append(parentId);
        sb.append(", name='").append(name).append('\'');
        sb.append(", url='").append(url).append('\'');
        sb.append(", icon='").append(icon).append('\'');
        sb.append(", level=").append(level);
        sb.append(", sequence=").append(sequence);
        sb.append(", parent=").append(parent);
        sb.append(", childs=").append(childs);
        sb.append('}');
        return sb.toString();
    }
}